package life.majiang.community.controller;

import life.majiang.community.dto.CommentDTO;
import life.majiang.community.dto.QuestionDTO;
import life.majiang.community.dto.ResultDTO;
import life.majiang.community.enums.CommentTypeEnum;
import life.majiang.community.exception.CustomizeErrorCode;
import life.majiang.community.exception.CustomizeException;
import life.majiang.community.model.CommentLikeCount;
import life.majiang.community.model.User;
import life.majiang.community.service.CommentService;
import life.majiang.community.service.LikeCountService;
import life.majiang.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by codedrinker on 2019/5/21.
 */
@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeCountService likeCountService;

    @GetMapping("/question/{id}")
    public String question(@PathVariable(name = "id") String id, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Long questionId = null;
        try {
            questionId = Long.parseLong(id);
        } catch (NumberFormatException e) {
            throw new CustomizeException(CustomizeErrorCode.INVALID_INPUT);
        }
        QuestionDTO questionDTO = questionService.getById(questionId);
        List<QuestionDTO> relatedQuestions = questionService.selectRelated(questionDTO);
        List<CommentDTO> comments = commentService.listByTargetId(questionId, CommentTypeEnum.QUESTION);
        List<CommentLikeCount> likeCounts = likeCountService.getCommentLikeCountDTOListByUserIdAndQuestionId(user.getId(), questionId);
//        为每个评论设置是否有点赞的
        for(CommentDTO commentDTO : comments){
            commentDTO.setLike(false);
            for(CommentLikeCount commentLikeCount : likeCounts){
                if(commentDTO.getId().equals(commentLikeCount.getCommentId())){
                    commentDTO.setLike(true);
                }
            }
        }
        //累加阅读数
        questionService.incView(questionId);
        model.addAttribute("question", questionDTO);
//        判断当前用户是否为该帖子的发起者，如果是，不显示匿名，否则根据是否匿名发帖显示是否匿名
        boolean flag;

        if(user.getId().equals(questionDTO.getUser().getId())){
//            查看者和发起者是同一个人
            flag = false;
        }else{
//            不是同一个人
            flag = questionDTO.getLikeCount() == 1;
        }
        //        匿名
        model.addAttribute("anonymous", flag);
        model.addAttribute("comments", comments);
//        for(CommentDTO commentDTO : comments){
//            System.out.println(commentDTO.getAnonymous());
//        }
        model.addAttribute("relatedQuestions", relatedQuestions);
        return "question";
    }

    @ResponseBody
    @DeleteMapping("/question/{id}")
    public ResultDTO deletePostById(@PathVariable(name = "id") String id){
        Long postId = null;
        try {
            postId = Long.parseLong(id);
        } catch (NumberFormatException e) {
            throw new CustomizeException(CustomizeErrorCode.INVALID_INPUT);
        }
        questionService.deletePostById(postId);

        return ResultDTO.okOf();
    }
}
